package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.api.CommonPage;
import com.example.demo.common.api.CommonResult;
import com.example.demo.config.MD5Util;
import com.example.demo.mapper.UserDao;
import com.example.demo.pojo.User;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class userController {
    @Autowired
    UserDao userDao;


    @ApiOperation("获取所有的人员信息")
    @RequestMapping(value = "getBrandList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<User>> getBrandList() {
        return CommonResult.success(userDao.selectList(null));
    }

    @ApiOperation("分页列表")
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<User>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1")
                                                    @ApiParam("页码") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "3")
                                                    @ApiParam("每页数量") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(userDao.selectList(null)));
    }

    @ApiOperation("添加一条信息")
    @PostMapping("/insert")
    public CommonResult insert(@RequestBody User user) {
        //通过MD5进行密码加密
        String pwd = MD5Util.md5(user.getPwd());
        user.setPwd(pwd);
        return CommonResult.success(userDao.insert(user));
    }

    @ApiOperation("批量添加数据")
    @PostMapping("/BatchInsert")
    public CommonResult BatchInsert(String[] nameAndPwd) {
        CommonResult commonResult = null;
        List<String> nameAndPwdList = Arrays.asList(nameAndPwd);
        User user = new User();
        for (String np : nameAndPwdList) {
            if (np != null) {
                /*批量添加数据标识符是@ 数据格式 name@pwd*/
                String[] strings = np.split("@");
                user.setName(strings[0]);
                user.setPwd(strings[1]);
                commonResult = CommonResult.success(userDao.insert(user));
            }
        }
        return commonResult;
    }

    @ApiOperation("修改信息,根据id修改信息-----updateById()")
    @PostMapping("/updateById")
    public CommonResult updateById(@RequestBody User user) {
        return CommonResult.success(userDao.updateById(user));
    }

    @ApiOperation("修改信息,使用构造器-----update()")
    @PostMapping("/update")
    public CommonResult update(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        //这个相当于where后面的条件
        //eq(R column, Object val)
        queryWrapper.eq("id", user.getId());
        return CommonResult.success(userDao.update(user, queryWrapper));
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteID(@PathVariable("id") Integer id) {
        return CommonResult.success(userDao.deleteById(id));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteByIds")
    public CommonResult deleteByIds(Long[] ids) {
        //数组转为集合
        List<Long> idList = Arrays.asList(ids);
        return CommonResult.success(userDao.deleteBatchIds(idList));
    }
}
